package com.c.yinyuezhushou.Dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.c.yinyuezhushou.Adapter.SongListAdapter1;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.R;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.Sql.MyDButil;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import java.util.List;

public class HomeDialog extends BottomSheetDialogFragment {
    private ImageView imageView;
    private TextView textView1;
    private TextView textView2;
    private MusicItem musicItem;
    private List<MusicItem> list;

    public HomeDialog(MusicItem musicItem, List<MusicItem> list) {
        this.musicItem = musicItem;
        this.list= list;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_home,container,false);
        MyApplication myApplication = (MyApplication) getActivity().getApplicationContext();
        MyDButil myDButil = myApplication.getMyDButil();
        imageView = view.findViewById(R.id.dh_image1);
        textView1 = view.findViewById(R.id.dh_text1);
        textView2 = view.findViewById(R.id.dh_text2);

        ImageView imageView2 = view.findViewById(R.id.dh_image2);
        TextView textView3  = view.findViewById(R.id.dh_text3);

        ImageView imageView3 = view.findViewById(R.id.dh_image3);
        TextView textView4  = view.findViewById(R.id.dh_text4);

        ImageView imageView4 = view.findViewById(R.id.dh_image4);
        TextView textView5  = view.findViewById(R.id.dh_text5);

        ImageView imageView5 = view.findViewById(R.id.dh_image5);
        TextView textView6  = view.findViewById(R.id.dh_text6);

        if (myDButil.isMusicItemTable(musicItem,"LoveMusicItem")){
            imageView3.setImageResource(R.drawable.icon_love1_1);
        }else {
            imageView3.setImageResource(R.drawable.icon_love);
        }

        View.OnClickListener downonClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (myApplication.getSetup().isDownAsk()){
                    DownDialog downDialog = new DownDialog(getContext(),true,null,musicItem);
                    downDialog.show();
                }else {
                    myApplication.getDownService().downloadMusicItem(musicItem,myApplication.getSetup().getDownbr());
                    Toast.makeText(myApplication, "正在飞速下载中...", Toast.LENGTH_SHORT).show();
                }
                HomeDialog.this.dismiss();
            }
        };

        View.OnClickListener playonClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myApplication.getMusicService().prepareToPlay(musicItem);
                myApplication.getMusicService().setMusicItems(list);
                HomeDialog.this.dismiss();
            }
        };

        View.OnClickListener loveonClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (myDButil.isMusicItemTable(musicItem,"LoveMusicItem")){
                    if (myDButil.removeMusicItemTable(musicItem,"LoveMusicItem")){
                        Toast.makeText(myApplication, "不爱了...", Toast.LENGTH_SHORT).show();
                        imageView3.setImageResource(R.drawable.icon_love);
                    }
                }else {
                    if (myDButil.setMusicItemTable(musicItem,"LoveMusicItem")){
                        Toast.makeText(myApplication, "爱了爱了", Toast.LENGTH_SHORT).show();
                        imageView3.setImageResource(R.drawable.icon_love1_1);
                    }
                }
                HomeDialog.this.dismiss();
            }
        };

        View.OnClickListener addonClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SongListHoemDialog songListHoemDialog = new SongListHoemDialog(getContext(),true,null,myApplication,musicItem);
                songListHoemDialog.show();
            }
        };

        imageView2.setOnClickListener(playonClickListener);
        textView3.setOnClickListener(playonClickListener);

        imageView3.setOnClickListener(loveonClickListener);
        textView4.setOnClickListener(loveonClickListener);

        imageView4.setOnClickListener(addonClickListener);
        textView5.setOnClickListener(addonClickListener);

        imageView5.setOnClickListener(downonClickListener);
        textView6.setOnClickListener(downonClickListener);


        Glide.with(getContext()).load(musicItem.getImageurl()).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(imageView);

        textView1.setText(musicItem.getSong());
        textView2.setText(musicItem.getSinger());
        return view;
    }


}
